package calcs;

import javax.swing.JTextArea;

public class OthersCalculator extends Thread {

	JTextArea res;
	int input, guide;

	public OthersCalculator(JTextArea res, int input, int guide, boolean debug) {
		this.res = res;
		this.input = input;
		this.guide = guide;
		if (!debug) {
			start();
		}
	}

	public void run() {
		switch (guide) {
		case 1:
			calculatePI();
			break;
		case 2:
			calculateFibbo();
			break;
		case 3:
			calculatePrime();
			break;
		}
	}

	public void calculatePI() {
		String result = "";
		if (input != 0) {
			int size = (int) Math.floor((input + 1) * 14 / 4);
			String x = "";
			int a, b, c, d, e, g, count;
			Double[] f = new Double[size + 1];
			a = 10000;
			b = 1;
			c = size;
			e = 0;

			while (b != c) {
				f[b] = Math.floor(a / 5);
				b++;
			}
			f[c] = 0.0;
			count = 1;
			while (c > 0) {
				g = 2 * c;
				d = 0;
				b = c;
				while (b > 0) {
					d = (int) (d + f[b] * a);
					g = g - 1;
					f[b] = (double) (d % g);
					d = (int) Math.floor(d / g);
					g = g - 1;
					b = b - 1;
					if (b != 0)
						d = d * b;
				}
				c = c - 14;
				int vahe = (int) (e + Math.floor(d / a));
				String vahe2 = Integer.toString(vahe);
				if (vahe2.length() < 4) {
					if (vahe2.length() == 3) {
						vahe2 = "0" + vahe2;
					} else if (vahe2.length() == 2) {
						vahe2 = "00" + vahe2;
					} else if (vahe2.length() == 1) {
						vahe2 = "000" + vahe2;
					} else if (vahe2.length() == 0) {
						vahe2 = "0000" + vahe2;
					}
				}
				x = vahe2;
				result += x;
				res.setText(result);
				if (count == 1) {
					String res1 = result.substring(0, 1);
					String res2 = result.substring(1, result.length());
					result = res1 + "." + res2;
				}
				count++;
				e = d % a;
			}
			result = result.substring(0, input + 2);
		} else {
			result = "3";
		}
		res.setText(result);
	}

	public void calculateFibbo() {
		int count;
		String result = "";
		String f1 = "0", f2 = "1", help = "";
		count = 2;
		if (input == 1) {
			result = "1) 0\n";
		} else {
			result = "1) 0\n2) 1\n";
			while (count < input) {
				help = adding(f1, f2);
				f1 = f2;
				f2 = help;
				count++;
				result += count + ") " + help + "\n";
				res.setText(result);
			}
		}
		result = result.substring(0, result.length() - 1);
		res.setText(result);
	}

	public void calculatePrime() {
		int p1 = 2;
		int count = 1;
		String result = "";
		boolean found = false;
		while (p1 <= input) {
			found = false;
			for (int i = 2; i < p1; i++) {
				if (p1 % i == 0) {
					found = true;
					break;
				}
			}
			if (!found) {
				result += count + ") " + p1 + "\n";
				res.setText(result);
				count++;
			}
			p1++;
		}
		result = result.substring(0, result.length() - 1);
		res.setText(result);
	}

	private static String adding(String a, String b) {
		String shorter, longer, help1 = "", res = "";
		int h1, h2, h3;
		boolean over = false;
		if (a.length() > b.length()) {
			shorter = b;
			longer = a;
		} else {
			shorter = a;
			longer = b;
		}
		if (shorter.length() != longer.length()) {
			do {
				shorter = "0" + shorter;
			} while (shorter.length() != longer.length());
		}
		a = shorter;
		b = longer;
		for (int i = 0; i < a.length(); i++) {
			h1 = Integer.valueOf(a.substring(a.length() - (i == 0 ? 1 : i + 1),
					a.length() - i));
			h2 = Integer.valueOf(b.substring(b.length() - (i == 0 ? 1 : i + 1),
					b.length() - i));
			if (over) {
				h3 = h1 + h2 + 1;
			} else {
				h3 = h1 + h2;
			}
			help1 = Integer.toString(h3);
			if (h3 >= 10) {
				help1 = help1.substring(1, help1.length());
				over = true;
			} else {
				over = false;
			}
			res = help1 + res;
		}
		if (over) {
			res = "1" + res;
		}
		return res;
	}
}
